package com.vrv.app.cyberspace.modules.sys.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.vrv.app.cyberspace.common.utils.JSONResult;
import com.vrv.app.cyberspace.common.validator.Assert;
import com.vrv.app.cyberspace.modules.sys.entity.BaseTable;
import com.vrv.app.cyberspace.modules.sys.service.IBaseTableService;
import com.vrv.app.cyberspace.modules.sys.service.IEventTableService;
import com.vrv.app.cyberspace.modules.sys.service.IRiskTableService;
import com.vrv.app.cyberspace.modules.sys.service.ITodayBulletinTableService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.vrv.app.cyberspace.common.utils.JSONResult.renderError;
import static com.vrv.app.cyberspace.common.utils.JSONResult.renderSuccess;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sm
 * @since 2017-12-15
 */
@Api(description = "基础信息")
@RestController
@RequestMapping("/sys/baseTable")
public class BaseTableController extends BaseController {

    @Autowired
    IBaseTableService baseTableService;
    @Autowired
    IRiskTableService riskTableService;
    @Autowired
    IEventTableService eventTableService;
    @Autowired
    ITodayBulletinTableService todayBulletinTableService;

    @ApiOperation(value = "获取信息详情", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回信息详情")
    })
    @GetMapping("/{id}")
    public JSONResult getBase(@PathVariable Integer id) {
        return baseTableService.getBase(id);
    }

    @ApiOperation(value = "变更信息状态为不采用", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回是否成功")
    })
    @PutMapping("/changeStatus")
    public JSONResult updateBaseInfo(String ids) {
        for (String id :
                ids.split(",")) {
            List<BaseTable> bts = new ArrayList<>();
            BaseTable baseTable = new BaseTable();
            baseTable.setId(Integer.valueOf(id));
            baseTable.setIsEnabled(3);
            bts.add(baseTable);
            baseTableService.updateBatchById(bts);
        }
        return renderSuccess();
    }

    @ApiOperation(value = "今日信息筛选", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回今日信息列表")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "条数", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "时间类型：1为创建时间 2为更新时间", dataType = "Integer", paramType = "query"),
    })
    @GetMapping()
    public JSONResult getListBase(Integer page, Integer size, String startTime, String endTime, String title, Integer type) {
        Assert.isNull(page, "page不能为空");
        Assert.isNull(size, "size不能为空");
        return baseTableService.getListBase(new Page<>(page, size), startTime, endTime, title, type);
    }

    @ApiOperation(value = "待处理信息数据统计", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回统计信息")
    })
    @GetMapping("/pendingCount")
    public JSONResult pendingCount() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pendingCount", baseTableService.selectCount(new EntityWrapper<BaseTable>().and("isEnabled=1")));
        return renderSuccess(jsonObject);
    }

    @ApiOperation(value = "不采用恢复为可用", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回成功信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "query"),
    })
    @PutMapping("/restore")
    public JSONResult restore(Integer id) {
        BaseTable old = baseTableService.selectById(id);
        if (old.getIsEnabled() != 3) {
            return renderError("操作被禁止，不可用从其它状态恢复！");
        }
        BaseTable baseTable = new BaseTable();
        baseTable.setId(id);
        baseTable.setIsEnabled(1);
        baseTable.updateById();
        return renderSuccess();
    }

    @ApiOperation(value = "数据统计汇总", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回汇总信息")
    })
    @GetMapping("/dataCount")
    public JSONResult dataCount() {
        return baseTableService.dataCount();
    }

    @ApiOperation(value = "安全动态", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回安全动态")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "条数", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/getDynamicInformationList")
    public JSONResult getDynamicInformationList(Integer page, Integer size) {
        Assert.isNull(page, "page不能为空");
        Assert.isNull(size, "size不能为空");
        return baseTableService.getDynamicInformationList(new Page<>(page, size));
    }

    @ApiOperation(value = "历史信息", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回历史信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "条数", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isEnabled", value = "1-已处理(不可用) 2-待采用(不可用)", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "时间类型：1为创建时间 2为更新时间", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/history")
    public JSONResult getListHistory(Integer page, Integer size, String startTime, String endTime, String title, Integer isEnabled, Integer type) {
        Assert.isNull(page, "page不能为空");
        Assert.isNull(size, "size不能为空");
        Assert.isNull(isEnabled, "isEnabled不能为空");
        return baseTableService.getListHistory(new Page<>(page, size), startTime, endTime, title, isEnabled, type);
    }
}

